<template>
	<view class="page">
		<!-- 头部 start -->
		<view class="cu-custom text-white bg-top fixed"
			:style="[{ height: CustomBar+54 + 'px' }]">
			<view class="cu-bar bg-top " :style="[{ height: CustomBar + 'px', paddingTop: StatusBar + 'px' }]">
				<view class="margin-left width-120 text-left" @tap="back"><text class="cuIcon-back"></text></view>
				<view class="">批量管理</view>
				<view class="right margin-right text-right width-120"> </view>
			</view>
			<fu-filter-dropdown :top="CustomBar" @changeLayout="changeLayout" @changeFilter="changeFilter"
				:filterKeyword="keywords" ref="childMethod" :cid="cid"></fu-filter-dropdown>
		</view>
		<!-- 头部 end -->
		<!-- 筛选 -->
		<!-- loading组件 -->
		<fu-loading v-if="isShowLoading"></fu-loading>
		<block v-else>
			<!--商品列表 start-->
			<view class="list padding-lr-24 relative padding-bottom-150 " :style="[{top:CustomBar+54+'px'}]">
				<view class="padding-top w100">
					<view class="flex align-center margin-bottom-sm bg-white padding-sm radius-16" v-for="(item, index) in lists"
						:key="index">
						<view @tap="checkItem(item, index)" class="margin-right-16">
							<checkbox class="round theme" :class="item.delChecked ? 'checked' : ''"
								:checked="item.delChecked ? true : false" style="transform: scale(0.7);"></checkbox>
						</view>
						<view class="w100">
							<view class="goods-info flex align-center " @tap="toDetail(item, index)">
								<view class=" relative margin-right-sm flex-shrink fu-block-200 radius-16 overHidden">
									<view class="box-kong padding-lr-16 padding-tb-4 text-white text-sm" v-if="item.port_name">
										{{item.port_name}}
									</view>
									<fu-image :src="item.thumb" mode="aspectFill"></fu-image>
								</view>
								<view class="height-200 flex  justify-between flex-direction w80">
									<view>
										<view class="text-df text-cut-2">{{ item.name }}
										</view>
										<view class="text-sm text-999 margin-top-sm sku_name">
											<view class="text-cut width-250">
												<text>商品货号：{{ item.sn }}</text>
											</view>
										</view>
									</view>
									<view class="desc-number flex justify-between align-center">
										<view class="text-lg text-prices flex-shrink" @tap="toDetail(item, index)">
											<text class="text-price"></text>
											<text>{{ item | filterGoodsPrice }}</text>
										</view>
										<view class="text-sm text-999 text-cut"> 销量{{item.sales_sum||0}} </view>
										<view class="text-sm text-999 text-cut"> 库存{{item.stock||0}} </view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="action-bar padding-lr cu-bar  bg-white flex justify-between" v-if="lists.length>0">
				<view class="flex flex-sub align-center   justify-between">
					<view class="flex justify-between align-center" @tap="checkAll">
						<checkbox class="round theme" :class="delAllChecked ? 'checked' : ''"
							:checked="delAllChecked ? true : false" style="transform: scale(0.7)"></checkbox>
						<text class="margin-left-sm text-df text-666">全选</text>
					</view>
				</view>
				<view class="flex align-center">
					<button class="cu-btn btn-settlement padding-lr-60 margin-right-sm radius-100 bg-f8 text-333 text-df "
						@click="delChoose">
						删除
					</button>
					<button class="cu-btn btn-settlement radius-100 bg-theme text-white text-df " @click="confirmAdd">
						批量{{this.selectIndex == 0?'下架':'上架'}}
					</button>
				</view>
			</view>
			<!--商品列表 end-->
			<fu-empty-ui v-if="lists.length == 0 && isInit"></fu-empty-ui>
		</block>
		<!-- 一键补仓弹框 -->
		<fu-popup v-model="isShowAdd" mode="center" border-radius="16" :mask-close-able="false">
			<view class="bg-white width-560">
				<view class="text-center padding text-lg text-333">一键补仓</view>
				<view class="padding-lr padding-bottom text-center">
					<view class="text-df margin-top-sm">已选{{totalNumber||0}}件商品</view>
					<view class="text-df margin-top-sm text-center ">
						<text>确认补仓至供应总量</text>
					</view>
				</view>
				<view class="foot solid-top flex align-center justify-between height-100">
					<view class="flex-sub text-gray solid-right text-center" @click="isShowStock=false">取消</view>
					<view class="flex-sub text-theme text-center" @click="handleConfirmAdd">确定</view>
				</view>
			</view>
		</fu-popup>
		<!-- 断网检测组件 -->
		<fu-notwork></fu-notwork>
	</view>
</template>
<script>
	// 基础筛选与高级筛选组件
	import fuFilterDropdown from '../../../goods/components/fu-filter-dropdown/fu-filter-dropdown.vue';
	// 楼层效果混入js
	import floor from '@/common/mixin/floor';
	export default {
		components: {
			fuFilterDropdown,
		},
		mixins: [floor],
		data() {
			return {
				timer: null, // 定时器
				isEditor: true, // 管理 or 完成
				type: 1, // 悬浮按钮状态 1 页码 2 返回顶部
				isShow: false, //是否显示按钮
				totalSize: 0, // 悬浮按钮总页码
				currentPage: 1, // 当前页页码
				isShowLoading: true, // 是否显示loading
				keywords: '', // 搜索的文本
				placeholder: global.i18n['请输入商品关键字'], // 默认文本
				model: 'block', // 布局样式
				lists: [], // 结果列表
				cid: '', // 分类id(点击分类跳转过来必传)
				isInit: false, // 是否请求过数据
				status: 'more', // 加载状态
				page: 1, // 页数
				size: 10, // 页条数
				sorts: '1', // 条件
				order: 'asc', // 排序 asc: 正序 desc: 倒序
				advancedSearch: null, // 高级搜索对象
				isShowStock: false, //库存弹框
				isShowAdd: false, //补仓弹框
				stockIpt: '', //库存数量
				stockItem: '', //选择的库存
				selectIndex: '', //0 出售中 1 审核中 2已下架
			};
		},
		computed: {
			// 全选状态
			delAllChecked() {
				let list = this.lists;
				let bool = true;
				list.forEach(item => {
					if (item.delChecked == false) {
						bool = false;
					}
				});
				return bool;
			},
			// 选择的商品数量
			totalNumber() {
				let list = this.lists;
				let total = 0;
				list.forEach(item => {
					if (item.checked == true) {
						total += Number(item.num);
					}
				});
				return total;
			},
		},
		watch: {
			lists: {
				handler: function(newVal, oldVal) {
					console.log(newVal, 111);
				},
				deep: true

			}
			// lists(newVal, oldVal) {
			// 	console.log('new', newVal, oldVal)
			// }
		},

		onLoad(options) {
			/***
			 * @keyWords 关键词
			 * @cid 分类id
			 **/
			const {
				keyWords = '', cid = '', selectIndex
			} = options;
			if (keyWords) {
				this.keywords = keyWords;
			}
			this.selectIndex = selectIndex
			if (cid != '') {
				this.cid = cid;
			}
			this.handleSearch(); // 搜索方法
		},
		onReachBottom() {
			this.handleSearch(); // 搜索方法
		},
		methods: {
			/**
			 * @event
			 * @description 去商品详情
			 */
			navToDetail(item) {
				this.$urouter.navigateTo({
					url: '/pages/goods/goodsdetail/goods-detail/index',
					params: {
						sku_id: item.sku_id || 0,
						goods_id: item.id
					}
				});
			},
			/**
			 * @description 切换布局
			 * @param {Object} e inline和block布局切换
			 */
			changeLayout(e) {
				this.model = e;
			},
			/**
			 * @description 切换搜索条件
			 * @param {Object} type 条件
			 * @param {Object} order  排序 asc: 正序 desc: 倒序
			 * @param {Object} advancedSearch 高级搜索对象
			 */
			changeFilter(type, order, advancedSearch) {
				this.order = order;
				this.page = 1;
				this.status = 'more';
				this.scrollInfo = []; // 清空楼层数量
				// 判断高级搜索存在

				this.handleSearch();
				this.$nextTick(() => {
					// 切换条件回到底部
					uni.pageScrollTo({
						scrollTop: 0,
						duration: 0
					});
				});
			},

			// 商品选中状态处理
			checkItem(item, i) {
				this.$set(this.lists[i], "delChecked", !item.delChecked)
			},

			//全选|取消全选
			checkAll() {
				const bool = !this.delAllChecked;
				this.lists.forEach(item => {
					// item.delChecked = bool;
					this.$set(item, 'delChecked', bool)
				});
				console.log(11, this.delAllChecked)
				this.$forceUpdate()
			},
			openPop(item, index) {
				this.isShowStock = true
				this.stockItem = item
			},
			// 确定输入库存弹框
			handleConfirm() {
				if (this.stockIpt <= 0) {
					return this.$message.info('库存不能小于0')
				}
				// if(this.stockIpt>){
				// 	return this.$message.info('剩余库存不足')
				// }
				this.isShowStock = false
			},
			// 一键补仓
			handleConfirmAdd() {
				this.isShowAdd = false
			},
			// 批量上架下架
			confirmAdd() {
				const list = this.lists;
				let ids = [];
				list.forEach(item => {
					if (item.delChecked) {
						ids.push(item.id);
					}
				});
				console.log(ids, 'ids');
				if (ids.length == 0) return this.$message.info(this.i18n['未选择商品']);
				this.$util.showModal({
					title: this.i18n['提示'],
					content: this.selectIndex == 0 ?'确定要下架该商品吗？':'确定要上架该商品吗？' ,
					confirmColor: this.$store.state.themeColor,
					success: res => {
						if (res.confirm) {
							this.$api
								.post('/v1/63dc7331c9770', {
									id: ids.join(','),
									is_sale:this.selectIndex == 0 ? 0:1,//0 下架 1上架
								})
								.then(res => {
									if(res.data.code==1){
										this.$message.info(this.selectIndex == 0 ?'下架成功':'上架成功');
										this.page = 1;
										this.status = 'more';										
										this.handleSearch();
									}
									
								})
								.catch(err => {
									console.log('err: ' + JSON.stringify(err));
								});
						}
					}
				});

			},
			//删除选中
			delChoose() {
				const list = this.lists;
				let ids = [];
				list.forEach(item => {
					if (item.delChecked) {
						ids.push(item.id);
					}
				});
				console.log(ids, 'ids');
				if (ids.length == 0) return this.$message.info(this.i18n['未选择商品']);
				this.$util.showModal({
					title: this.i18n['提示'],
					content: '商品出售中，删除后未购买的用户不可见，已购买的用户不受影响 确定删除当前内容？',
					confirmColor: this.$store.state.themeColor,
					success: res => {
						if (res.confirm) {
							this.$api
								.post('/v1/63dc72b9dfed0', {
									id: ids.join(',')
								})
								.then(res => {
									console.log('res: ' + JSON.stringify(res));
									this.getCartData();
									this.$store.dispatch('getCartList');
								})
								.catch(err => {
									console.log('err: ' + JSON.stringify(err));
								});
						}
					}
				});
			},
			toDetail(item, index) {
				this.$urouter.navigateTo({
					url: '/pages/goods/goodsdetail/goods-detail/index',
					params: {
						"id": item.id,
					}
				})
			},
			
			/**
			 * @func 搜索
			 */
			handleSearch() {
				if (this.status != 'more') return;
				this.status = 'loading';
				let data = {
					type: this.selectIndex == 0 ? 'sale' : this.selectIndex == 1 ? 'check' :
					'down', //类型  sale：出售中 check：审核中 down：已下架				
					page: this.page,
					list_rows: this.size,
				};
				
				// 判断关键字或者placeholder内容
				if (this.placeholder.trim() || this.keywords.trim()) {
					data.keyword = this.keywords;
					this.addHistory(this.keywords.trim());
				}
				// if(this.keywords.trim() == ''){
				//   this.$message.info("请输入搜索关键字");
				//   this.lists=[];
				//   return
				// }
				if (this.page == 1) {
					this.isShowLoading = true;
				}
				// 获取商品列表接口
				this.$api
					.post('/v1/63db775cd4a5a', data)
					.then(res => {
						this.isShowLoading = false;
						if (res.data.code == 1) {
							let totalSize = res.data.data.total;
							this.totalSize = Math.ceil(totalSize / 10); // 悬浮按钮总页码
							this.currentPage = this.page; //悬浮当前页面
							let list = res.data.data.data;
							if (this.page == 1) this.lists = [];
							list=list.map(item => {
								let obj = {};
								obj = {
									...item,
									delChecked: false
								}
								return obj;
							})
							this.lists = this.lists.concat(list);
							if (this.lists.length >= totalSize) {
								this.status = 'noMore';
							} else {
								this.status = 'more';
								this.page++;
							}
							this.isInit = true;
						} else {
							this.$message.error(res.data.msg);
						}
					})
					.catch(err => {
						this.isShowLoading = false;
					});
			},
			
			// 返回
			back() {
				uni.navigateBack();
			},
			/**
			 * @description 增加一条搜索历史记录
			 * @param {Object} value 关键词
			 */
			addHistory(value) {
				if (!value) return;
				let _history = uni.getStorageSync('SEARCHHISTORY');
				if (!_history) {
					_history = [];
				}
				_history.unshift(value);
				let history = [...new Set(_history)];
				uni.setStorageSync('SEARCHHISTORY', history);
			}
		}
	};
</script>

<style lang="scss" scoped>
	page {
		height: 100%;
	}

	.bg-top {
		background: linear-gradient(90deg, #FFBE65 0%, #F97F33 100%);
	}

	.fixed {
		position: fixed;
		top: 0;
		width: 100%;
		z-index: 100;
	}

	.page {
		min-height: 100%;
		/* background-color: #ffffff; */
	}

	.head-top {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
	}

	.nav-bar-wrap {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
	}

	.sort-wrap {
		position: fixed;
		left: 0;
		right: 0;
	}

	.height-100 {
		height: 100rpx;
	}

	.block-180 {
		width: 180rpx;
		min-width: 180rpx;
		height: 180rpx;
		min-height: 180rpx;
		position: relative;

		image {
			width: 100%;
			height: 100%;
			border-radius: 8rpx;
		}

		.null {
			position: absolute;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			border-radius: 8rpx;
			background-color: rgba(0, 0, 0, 0.45);
			color: #ffffff;
			font-size: 36rpx;
			text-align: center;
			line-height: 180rpx;
		}
	}

	.list-item+.list-item {
		border-top: 1rpx solid #eeeeee;
	}

	.overlay-animate {
		position: fixed;
	}

	.float-action {
		width: 72rpx;
		height: 72rpx;
		border-radius: 50%;
		// background-color: rgba(165, 165, 165, .85);
		position: fixed;
		right: 20rpx;
		bottom: 280rpx;
		z-index: 996;
		border: 1rpx solid #cccccc;

		.cuIcon-cart {
			color: #cccccc;
		}
	}

	.animate {
		position: fixed;
		border-radius: 50%;
		z-index: 995;
		transform-origin: 50% 50%;
		transition: transform linear 0.5s, left linear 0.5s, top cubic-bezier(0.3, -0.2, 1, 0) 0.5s !important;

		image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
		}
	}

	// 商品卡片line
	.fu-goods-line {
		/* min-height: 570rpx; */
		padding: 24rpx 0;
		border-radius: 24rpx;
		/* background-color: #ffffff; */
		// margin-top: 20rpx;
		display: flex;

		.block-246 {
			padding: 24rpx 24rpx 0;

			.img {
				width: 172rpx;
				height: 172rpx;
				border-radius: 6rpx;
				background-color: #eeeeee;
			}
		}

		.content {
			flex: 1;
			overflow: hidden;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		}
	}

	// 商品卡片block
	.fu-goods-block {
		overflow: hidden;
		margin-bottom: 20rpx;
		padding: 0 10rpx;

		.wrap-box {
			background-color: #ffffff;
			border-radius: 16rpx;

			.square-wrap {
				width: 100%;
				height: 0;
				padding-bottom: 100%;
				box-sizing: border-box;
				position: relative;

				.square-box {
					position: absolute;
					left: 0;
					top: 0;
					right: 0;
					bottom: 0;
					border-radius: 24rpx 24rpx 0 0;

					image {
						width: 100%;
						height: 100%;
						border-radius: 24rpx 24rpx 0 0;
					}
				}
			}

			.image {
				width: 100%;
				border-radius: 24rpx 24rpx 0 0;
			}
		}

		.content {
			padding: 20rpx;
		}
	}

	.item-activity-label {
		padding: 2rpx 5rpx;
		/* margin: 5rpx; */
		font-size: 24rpx;
		border-radius: 6rpx;
		border: 1px solid;
		margin: 0rpx 1px 0px;
		display: inline-block;
		transform: scale(0.8);
	}

	.padding-lr-24 {
		padding: 0 24rpx;
	}

	.padding-lr-4 {
		padding: 0 4rpx;
	}

	.search-form {
		margin-left: 12rpx;
	}

	.item-goods-label {
		padding: 2rpx 8rpx;
		color: #ff6464;
		font-size: 24rpx;
		margin-right: 6rpx;
		background: rgba(255, 100, 100, 0.1);
		/* opacity: 0.1; */
		border-radius: 4rpx;
	}

	.item-activity-label {
		padding: 0 8rpx;
		height: 32rpx;
		line-height: 30rpx;
		border: 1px solid #fa2033;
		opacity: 1;
		border-radius: 4rpx;
		font-size: 20rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #fa2033;
		margin-right: 16rpx;

		&:nth-of-type(1) {
			border: 1px solid #0a7ce6;
			color: #0a7ce6;
		}
	}

	.sales-number {
		margin-top: 16rpx;
	}

	.through-price {
		margin-left: 16rpx;
		color: #bfbfbf;
	}

	.box-kong {
		position: absolute;
		border-radius: 16rpx 0 16rpx 0;
		left: 0;
		top: 0;
		z-index: 99;
		background: rgba(0, 0, 0, 0.6);
	}

	.action-bar {
		position: fixed;
		bottom: 0 !important;
		width: 100%;
		z-index: 999;
	}
</style>